// $codepro.audit.disable numericLiterals
/**
 * This file was created by Nick Popescu, Andrew Darrohn GameScreen for
 * use in InitConfigScreen
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;

/**
 * This is the main Game Screen GUI. It displays the main GUI components of the
 * game, like the Map, the store, etc.
 * 
 * @author Nick Popescu
 * @version 1.0
 * 
 */
public class GameScreen extends JPanel implements MouseListener {
    
    /**
     * This is the space.
     */
    private final Space space;
    
    /**
     * This is the player.
     */
    private final Player player;
    
    /**
     * This is the poly for the space map.
     */
    private final Polygon spaceMapPolygon;
    
    /**
     * The poly for the market.
     */
    private final Polygon marketPolygon;
    
    /**
     * The space port poly.
     */
    private final Polygon spacePortPolygon;
    
    /**
     * The options poly.
     */
    private final Polygon optionsPolygon;
    
    /**
     * The X coords for space map poly.
     */
    private final int[] spaceMapPolyX = new int[] { 78, 201, 201, 78 }; // specifies
								  // the
								  // bounds of
								  // the Space
								  // button.
    
    /**
     * The Y coords on space map poly.
     */
    private final int[] spaceMapPolyY = new int[] { 283, 283, 369, 369 };
    
    /**
     * The X coords for the market poly.
     */
    private final int[] marketPolyX = new int[] { 43, 103, 103, 43 };
    
    /**
     * The Y coords for the maket poly.
     */
    private final int[] marketPolyY = new int[] { 102, 102, 130, 130 };
    
    /**
     * Space Port Poly X coords.
     */
    private final int[] spacePortPolyX = new int[] { 43, 102, 102, 43 };
    
    /**
     * The Y coords for space port poly.
     */
    private final int[] spacePortPolyY = new int[] { 148, 148, 171, 171 };
    
    /**
     * The X coords for the options poly.
     */
    private final int[] optionsPolyX = new int[] { 470, 530, 530, 470 };
    
    /**
     * The Y coords for the options poly.
     */
    private final int[] optionsPolyY = new int[] { 150, 150, 176, 176 };
    
    /**
     * The popUp JPanel.
     */
    private final JPanel popUpPanel;
    
    /**
     * The space map screen.
     */
    private SpaceMapScreen spaceMapPanel;
    
    /**
     * The market panel.
     */
    private MarketplaceScreen marketPanel;
    
    /**
     * The space mort panel.
     */
    private SpacePortScreen spacePortPanel;
    
    /**
     * The options panel.
     */
    private OptionsScreen optionsPanel;
    
    /**
     * The current pop up screen.
     */
    private String currentPopUp = ""; // A String representation of what the
				      // current POP up window's name is.
    
    /**
     * The player location label.
     */
    private final JLabel playerLocationLabel;
    
    /**
     * The fuel label.
     */
    private final JLabel fuelLabel;
    
    /**
     * The money label.
     */
    private final JLabel moneyLabel;
    // private JLabel cargoLabel;
    // private Rectangle saveRect = new Rectangle(237, 280, 100, 85);
    
    /**
     * The Cargo label.
     */
    private final JLabel cargoLabel;

    /**
     * Create the the Game Screen panel.
     * 
     * @param space
     *            This is the Space grid that has been made and should be
     *            displayed.
     *            
     * @param player
     * 		  The player of the game.
     */
    public GameScreen(Space space, Player player) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
	this.space = space;
	this.player = player;

	setLayout(null);

	playerLocationLabel = new JLabel(
		"<html>\r\n<h3>New label</h3>\r\n<html>");
	playerLocationLabel.setBackground(Color.WHITE);
	playerLocationLabel.setBounds(148, 25, 220, 26);
	final Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);// Sets the font of
							     // the smaller
							     // text.

	playerLocationLabel.setFont(font);
	playerLocationLabel.setForeground(Color.ORANGE);
	playerLocationLabel.setText("Currently On "
		+ Game.Player.getCurrentPlanet().getName());

	add(playerLocationLabel);

	fuelLabel = new JLabel("<dynamic>");
	fuelLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
	fuelLabel.setBackground(Color.WHITE);
	fuelLabel.setBounds(418, 279, 220, 26);
	add(fuelLabel);

	moneyLabel = new JLabel("<dynamic>");
	moneyLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
	moneyLabel.setBackground(Color.WHITE);
	moneyLabel.setBounds(418, 320, 220, 26);
	add(moneyLabel);

	cargoLabel = new JLabel("<dynamic>");
	cargoLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
	cargoLabel.setBackground(Color.WHITE);
	cargoLabel.setBounds(418, 338, 220, 26);
	add(cargoLabel);

	popUpPanel = new JPanel(); // This is the Panel that is in the middle of
				   // the screen.
	popUpPanel.setBounds(148, 81, 280, 180);
	add(popUpPanel);
	popUpPanel.setLayout(new BoxLayout(popUpPanel, BoxLayout.X_AXIS));
	popUpPanel.setOpaque(false);

	final JLabel lblNewLabel = new JLabel("New label");
	lblNewLabel.setIcon(new ImageIcon("imgs//gameScreen2.png"));
	lblNewLabel.setBounds(0, 0, 568, 408);
	add(lblNewLabel);

	// The following is the polygon that overlays on the Space Map button.
	spaceMapPolygon = new Polygon(spaceMapPolyX, spaceMapPolyY,
		spaceMapPolyX.length);
	marketPolygon = new Polygon(marketPolyX, marketPolyY,
		marketPolyX.length);
	spacePortPolygon = new Polygon(spacePortPolyX, spacePortPolyY,
		spacePortPolyX.length);
	optionsPolygon = new Polygon(optionsPolyX, optionsPolyY,
		optionsPolyX.length);

	addMouseListener(this);
    }

    /**
     * This is responsible for drawing any visual things to the Panel. Currently
     * does nothing.
     * 
     * @param g The Graphics ref.
     */

    @Override
    public void paintComponent(Graphics g) {
	super.paintComponent(g);

	playerLocationLabel.setText("Currently On "
		+ Game.Player.getCurrentPlanet().getName());
	fuelLabel.setText(Game.Player.getShip().getCurrFuelLevel() + "/"
		+ Game.Player.getShip().getThisFuelSize());

	moneyLabel.setText("$ " + Game.Player.getWallet());

	cargoLabel.setText(Game.Player.getShip().getTotalCargo() + "/"
		+ Game.Player.getShip().getThisCapacity());

	/**
	 * Graphics2D g2d = (Graphics2D) g;
	 * 
	 * 
	 * 
	 * Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);//Sets the font
	 * of the text. g.setFont(font); g.setColor(Color.white);
	 * g2d.drawString(Game.Player.getCurrentPlanet().getName(),148,56);
	 */
	// g2d.setColor(Color.white);

	// g2d.drawRect(5, 30, 30, 20);
    }

    /**
     * Used to register if the mouse is clicked. Checks if the mouse clicks any
     * buttons.
     * 
     * @param e The Event.
     */

    @Override
    public void mouseClicked(MouseEvent e) {
	System.out.println("X: " + e.getX() + ", Y: " + e.getY());
	if (spaceMapPolygon.contains(e.getPoint())) { // Is the Space Map button
						      // clicked?
	    if (!currentPopUp.equals("Space Map")) {

		popUpPanel.setVisible(true);

		spaceMapPanel = new SpaceMapScreen(space);
		// spaceMapPanel.setBounds(148, 81, 280, 180);
		spaceMapPanel.setBounds(148, 81, 280, 180);

		popUpPanel.removeAll();
		popUpPanel.add(spaceMapPanel);

		spaceMapPanel.setVisible(true);
		popUpPanel.revalidate();
		popUpPanel.repaint();
		currentPopUp = "Space Map";
		// System.out.println("ADDED");
	    } else {
		currentPopUp = "";
		spaceMapPanel.setVisible(false);
	    }
	    // repaint();
	    // System.out.println("X: "+e.getX()+", Y: "+e.getY());
	}

	if (marketPolygon.contains(e.getPoint())) { // Is the Space Map button
						    // clicked?
	    if (!currentPopUp.equals("Marketplace")) {

		popUpPanel.setVisible(true);

		marketPanel = new MarketplaceScreen();
		// spaceMapPanel.setBounds(148, 81, 280, 180);
		marketPanel.setBounds(148, 81, 280, 180);
		popUpPanel.removeAll();
		popUpPanel.add(marketPanel);

		marketPanel.setVisible(true);
		popUpPanel.revalidate();
		popUpPanel.repaint();
		currentPopUp = "Marketplace";
		// System.out.println("ADDED");
	    } else {
		currentPopUp = "";
		marketPanel.setVisible(false);
	    }
	    // repaint();
	    // System.out.println("X: "+e.getX()+", Y: "+e.getY());
	}

	if (spacePortPolygon.contains(e.getPoint())) { // Is the Space Map
						       // button clicked?
	    if (!currentPopUp.equals("SpacePort")) {

		popUpPanel.setVisible(true);

		spacePortPanel = new SpacePortScreen();
		// spaceMapPanel.setBounds(148, 81, 280, 180);
		spacePortPanel.setBounds(148, 81, 280, 180);
		popUpPanel.removeAll();
		popUpPanel.add(spacePortPanel);

		spacePortPanel.setVisible(true);
		popUpPanel.revalidate();
		popUpPanel.repaint();
		currentPopUp = "SpacePort";
		// System.out.println("ADDED");
	    } else {
		currentPopUp = "";
		spacePortPanel.setVisible(false);
	    }
	    // repaint();
	    // System.out.println("X: "+e.getX()+", Y: "+e.getY());
	}

	if (optionsPolygon.contains(e.getPoint())) { // Is the Space Map button
						     // clicked?
	    if (!currentPopUp.equals("Options")) {

		popUpPanel.setVisible(true);

		optionsPanel = new OptionsScreen();
		optionsPanel.setBounds(148, 81, 280, 180);
		popUpPanel.removeAll();
		popUpPanel.add(optionsPanel);

		optionsPanel.setVisible(true);
		popUpPanel.revalidate();
		popUpPanel.repaint();
		currentPopUp = "Options";
	    } else {
		currentPopUp = "";
		optionsPanel.setVisible(false);
	    }
	}

	// if(saveRect.getBounds().contains(e.getPoint())){
	// SaveLoad save = new SaveLoad();
	// save.save(Game.saveFilename);
	// System.out.println("Saved!");
	// }

	repaint();
    }

    /**
     * This is unused, but required by the MouseListener.
     * 
     * @param e the mouse event
     */

    @Override
    public void mousePressed(MouseEvent e) {
	System.out.print("");
    }

    /**
     * This is unused, but required by the MouseListener.
     * 
     * @param e The mouse event
     */
    @Override
    public void mouseReleased(MouseEvent e) {
	System.out.print("");
    }

    /**
     * This is unused, but required by the MouseListener.
     * 
     * @param e The mouse event.
     */
    @Override
    public void mouseEntered(MouseEvent e) {
	System.out.print("");
    }

    /**
     * This is unused, but required by the MouseListener.
     * 
     * @param e The Mouse event.
     */

    @Override
    public void mouseExited(MouseEvent e) {
	System.out.print("");
    }
}
